<?php
include_once 'protoweb_controller.php';

/**--------------------------------------------------------------------------
 * Please, don't touch this file.
 *
 * If you need to custom this Class, you can edit clients.php
 * file overwrite and define new methods
 *---------------------------------------------------------------------------
 */

/**
 * @package Protoweb
 * @subpackage controller
 */
class Clients_protoweb extends Protoweb_controller {

  public function __construct() {
    parent::__construct();
  } // end of method __construct()



  public function edit($id=0) {
    $data["client"] = new Client($id);

    if ($this->input->post("posting") == 1)
      $data = $this->edit_save($data["client"]->id);

    // To one relationships
        
    $services = new Service();
    $data["services"] = $services->all_to_asoc_array("id","name");
    $data["services_selected"] = $data["client"]->service->id;
    
    $this->smarty_parser->view('client_edit.html', $data);
  } // end of method edit($id=0)


  private function edit_save($id) {
    $data["error"] = "";
    $data["client"] = new Client($id);

          // Assign value to field. If value is empty, we will assign NULL
      $data["client"]->name = $this->input->post("name");
      if ($data["client"]->name == "") $data["client"]->name = NULL;
      
      // files for upload
      /*
      if ($data["client"]->name->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(name)) $data["client"]->name = $fileupload.clean_file_name(name);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["client"]->country = $this->input->post("country");
      if ($data["client"]->country == "") $data["client"]->country = NULL;
      
      // files for upload
      /*
      if ($data["client"]->country->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(country)) $data["client"]->country = $fileupload.clean_file_name(country);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["client"]->state = $this->input->post("state");
      if ($data["client"]->state == "") $data["client"]->state = NULL;
      
      // files for upload
      /*
      if ($data["client"]->state->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(state)) $data["client"]->state = $fileupload.clean_file_name(state);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["client"]->location = $this->input->post("location");
      if ($data["client"]->location == "") $data["client"]->location = NULL;
      
      // files for upload
      /*
      if ($data["client"]->location->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(location)) $data["client"]->location = $fileupload.clean_file_name(location);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["client"]->zipcode = $this->input->post("zipcode");
      if ($data["client"]->zipcode == "") $data["client"]->zipcode = NULL;
      
      // files for upload
      /*
      if ($data["client"]->zipcode->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(zipcode)) $data["client"]->zipcode = $fileupload.clean_file_name(zipcode);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["client"]->address = $this->input->post("address");
      if ($data["client"]->address == "") $data["client"]->address = NULL;
      
      // files for upload
      /*
      if ($data["client"]->address->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(address)) $data["client"]->address = $fileupload.clean_file_name(address);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["client"]->lat = $this->input->post("lat");
      if ($data["client"]->lat == "") $data["client"]->lat = NULL;
      
      // files for upload
      /*
      if ($data["client"]->lat->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(lat)) $data["client"]->lat = $fileupload.clean_file_name(lat);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["client"]->lng = $this->input->post("lng");
      if ($data["client"]->lng == "") $data["client"]->lng = NULL;
      
      // files for upload
      /*
      if ($data["client"]->lng->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(lng)) $data["client"]->lng = $fileupload.clean_file_name(lng);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["client"]->email = $this->input->post("email");
      if ($data["client"]->email == "") $data["client"]->email = NULL;
      
      // files for upload
      /*
      if ($data["client"]->email->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(email)) $data["client"]->email = $fileupload.clean_file_name(email);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["client"]->phone = $this->input->post("phone");
      if ($data["client"]->phone == "") $data["client"]->phone = NULL;
      
      // files for upload
      /*
      if ($data["client"]->phone->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(phone)) $data["client"]->phone = $fileupload.clean_file_name(phone);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["client"]->slug = $this->input->post("slug");
      if ($data["client"]->slug == "") $data["client"]->slug = NULL;
      
      // files for upload
      /*
      if ($data["client"]->slug->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(slug)) $data["client"]->slug = $fileupload.clean_file_name(slug);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
    
    //saving attributes
    if(! $data["client"]->save())
      $data["error"] .= $data["client"]->error->string;

    //saving to one relationships
    
    $service = new Service($this->input->post("service"));
    if (! $data["client"]->save(array($service)))
      $data["error"] .= $data["client"]->error->string;
    
    //to many relationships
                                                            
    redirect(base_url()."clients/edit/".$data["client"]->id);
  } // end of method edit_save($id)


  public function delete($id) {
    $data["error"] = "";
    $data["client"] = new Client($id);

    //delete
    if(! $data["client"]->delete())
      $data["error"] .= $data["client"]->error->string;

    redirect(base_url()."clients/edit/");
  } // end of method delete($id)

    
} // class Clients_protoweb

/* End of file clients_protoweb.php */
/* Location: ./system/application/controllers/clients_protoweb.php */
